package shiyl.usejava8lambda.shiyl.usejava8stream.cases;

import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ResultTreeType;

import java.util.ArrayList;
import java.util.List;

/**
 * 对java8中Stream的简单Demo示意
 * @author shiyl
 */
public class SimpleStreamDemo {


    public static final void main( String [] agrs){

        List<DemoUser> users = new ArrayList<>();
        users.add(new DemoUser("xiaoA", 11, "male"));
        users.add(new DemoUser("xiaoB", 12, "female"));
        users.add(new DemoUser("xiaoC", 13, "male"));
        users.add(new DemoUser("xiaoD", 14, "male"));
        users.add(new DemoUser("xiaoE", 15, "male"));
        users.add(new DemoUser("xiaoF", 16, "female"));
        users.add(new DemoUser("xiaoG", 17, "male"));
        users.add(new DemoUser("xiaoH", 18, "female"));
        users.add(new DemoUser("xiaoI", 19, "male"));
        users.add(new DemoUser("xiaoJ", 20, "female"));

        // 使用传统For遍历，选出所有female
        for ( DemoUser u : users ){
           if( u.getSex().equals("female" )){
               System.out.println(u);
           }
        }

        System.out.println("==========================");

        // 使用Java8的Stream进行遍历，选出所有 female
        users.stream().filter(user->user.getSex().equals("female")).forEach(user -> System.out.println(user));
    }
}
